﻿using UnityEngine;
using System.Collections;

public class statMenu : MonoBehaviour
{
    private int index = 0;
    private int fragments;
    private int initialFragments;
    private GUIStyle toggleHighlight;
    public stats[] statList;
    private Rect textboxLoc = new Rect(100, 200, 600, 400);
    public Texture2D plusBoxTexture, backgroundTex, statsBackground, hightlight;
    private Rect plusBoxLoc = new Rect(400, 400, 100, 100);
    private Rect minusBoxLoc = new Rect(200, 400, 100, 100);
    private Rect valueBoxLoc = new Rect(300, 300, 100, 80);
    
    public static bool statStart = false;
    private bool DoOnce = true;
    public GUIStyle statMenuStyle, referenceStyle, boxOutline, highlightStyle;
    bool temp = false;

    [System.Serializable]
    public class stats
    {
        //private string button1 = "re";
        public string name = "Insert Name";
        public int value = 0;
        public string text = "text";
        public bool autoplace = true;
        public ValueAttr ValueAttributes;
        public Rect MenuButtonLocation = new Rect(110, 110, 80, 80);
        public string playerPrefKey = "PlayerPrefKey";
        public Texture2D tabTexture;

    }
    [System.Serializable]
    public class ValueAttr
    {
        public int maxVal = 100;
        public int minVal = 0;
        public int increment = 10;
        public int Cost = 10;
    }

    void Start()
    {
        index = 0;
        initialFragments = PlayerPrefs.GetInt("Currency");
        toggleHighlight = new GUIStyle();
        toggleHighlight.onNormal.textColor = Color.red;
        fragments = PlayerPrefs.GetInt("Currency");
        statStart = false;
    }

    public void DoGUI()
    {
        referenceStyle = new GUIStyle(GUI.skin.button);
        Rect Overallbox = new Rect(Screen.width / 2 + textboxLoc.x, Screen.height * .2f, Screen.width*.9f, Screen.height * .5f);
        Overallbox = new Rect(Screen.width / 2 - Overallbox.width/2, Screen.height * .3f, Overallbox.width, Overallbox.height);

        GUIStyle labelStyle = new GUIStyle(GUI.skin.GetStyle("label"));
        labelStyle.fontSize = 60;
        //labelStyle.font = weaponScreenText.font;
        labelStyle.normal.textColor = Color.white;
        labelStyle.alignment = TextAnchor.MiddleCenter;
        GUIStyle labelStyle2 = new GUIStyle(GUI.skin.GetStyle("box"));
        labelStyle2.fontSize = 35;
        


        GUI.DrawTexture(Overallbox, backgroundTex);
        GUI.Box(Overallbox, statList[index].text, labelStyle2);

        //Value Box
        float ValueBoxH = Overallbox.height * .5f;
        float ValueBoxW = Overallbox.width * .6f;

        var statBoxStyle = statMenuStyle;
        boxOutline.normal.background = statsBackground;
        boxOutline.onNormal.background = referenceStyle.onNormal.background;
        //statBoxStyle.normal.background = statsBackground;
        //statBoxStyle.onNormal.background = referenceStyle.onNormal.background;
        //statBoxStyle.onActive.background = referenceStyle.onActive.background;
        //statBoxStyle.onHover.background = referenceStyle.onHover.background;
        //statBoxStyle.onFocused.background = referenceStyle.onFocused.background;
        

        Rect ValueBox = new Rect(Overallbox.x + Overallbox.width/2 - ValueBoxW/2, Overallbox.y + Overallbox.height*.4f - ValueBoxH/2, ValueBoxW, ValueBoxH);
        
        GUIStyle sublabel = new GUIStyle(GUI.skin.GetStyle("box"));
        sublabel.fontSize = 30;
        GUI.Box(ValueBox, "Upgrade Cost: " + statList[index].ValueAttributes.Cost + "\n\n\n" + statList[index].value.ToString(), sublabel);

        Rect tabs = new Rect(Overallbox.x, Overallbox.y - Overallbox.height, Overallbox.width / 6, Screen.height * .15f);

        if (statStart)
        {
            Start();
        }
        for (int i = 0; i < statList.Length; i++)
        {
            statBoxStyle.normal.background = statList[i].tabTexture;
                tabs = new Rect(Overallbox.x + tabs.width * i, Overallbox.y - tabs.height, Overallbox.width / 6, Screen.height * .15f);
                GUI.Box(tabs, "", statBoxStyle);

                //temp = GUI.Toggle(tabs, temp, "", boxOutline);
                if (GUI.Button(tabs,"", boxOutline))
                {

                    index = i;
                }
               
        }
        highlightStyle.normal.background = hightlight;
        GUI.color = new Color(1, 1, 1, .6f);
        highlightStyle.border = new RectOffset(3, 3, 3, 3);
        Rect tabs2 = new Rect(Overallbox.x + tabs.width * index, Overallbox.y - tabs.height, Overallbox.width / 6, Screen.height * .15f);
        GUI.Box(tabs2, "", highlightStyle);
        GUI.color = new Color(1, 1, 1, 1);

        GUI.Label(new Rect(Screen.width / 2 - statList[3].MenuButtonLocation.width * 4, Screen.height*.08f - statList[3].MenuButtonLocation.height/2, statList[3].MenuButtonLocation.width * 8, statList[3].MenuButtonLocation.height*1.5f), "Ship Stats", labelStyle);
        statList[index].text = statList[index].text.Replace("___", "\n");
		fragments = PlayerPrefs.GetInt("Currency");
        if (GUI.Button(new Rect(ValueBox.x + ValueBox.width/2 - ValueBoxW*.4f, Overallbox.y + Overallbox.height *.8f - ValueBoxH/4, ValueBoxW*.8f, ValueBoxH/2), plusBoxTexture) && fragments > 0 && fragments >= statList[index].ValueAttributes.Cost)
        {
            fragments = PlayerPrefs.GetInt("Currency");
            if (statList[index].value < statList[index].ValueAttributes.maxVal)
            {
                statList[index].value += statList[index].ValueAttributes.increment;
                fragments -= statList[index].ValueAttributes.Cost;
                PlayerPrefs.SetInt("Currency", fragments);
                PlayerPrefs.SetInt(statList[index].playerPrefKey, statList[index].value);
                print(statList[index].playerPrefKey);
            }
        }

        if (statList[index].value > statList[index].ValueAttributes.maxVal)
            statList[index].value = statList[index].ValueAttributes.maxVal;
    }
}